<template>
	<view>
		<fa-navbar title="" :isBack="false"></fa-navbar>
		<view class="px-2 pt-5 fa-user-form">
			<u-form :model="formData" ref="uForm">
				<u-form-item label="" left-icon=" iconfont icon-shouji" :border-bottom="false">
					<u-input v-model="formData.phone" :custom-style="{'font-size': '32rpx'}" height="80" placeholder="输入手机号码" maxlength="11" type="number"/>
					<!-- <u-input v-model="formData.phone" :custom-style="{'font-size': '32rpx'}" height="80" placeholder="输入手机号码" maxlength="11" type=""/> -->
				</u-form-item>
				<u-form-item label="" left-icon=" iconfont icon-mima" :border-bottom="false">
					<u-input onpaste="return false" :custom-style="{'font-size': '32rpx'}" v-model="formData.password" height="80" placeholder="输入密码" maxlength="12" type="password"/>
				</u-form-item>
			</u-form>
			<view class="text-right pr-1" @click="toPage('./register')" style="color: #666666;">注册</view>
			<view class="pt-4">
				<u-button 
					:type="!formValid ? 'default' : 'primary'"
					shape="circle"
					class="btn-grad" 
					:class="formValid ? '' : 'btn-disable'"
					:disabled="!formValid"
					@click="login"
				>登录</u-button>
			</view>
			<view class="font24 text-center col-gray pt-2">
				登录代表您同意<text @click="toPage('../user/about/serviceAgreement')" class="h4-font-royal-blue">《用户服务协议》</text>
			</view>
		</view>
		<u-modal
			v-model="showAgreen"
			title="感谢您使用我们的应用"
			:title-style="{'font-size': '32rpx', 'color': '#2A2A2A'}"
			:content-style="{'font-size': '28rpx', 'color': '#999999'}"
			:cancel-style="{'font-size': '28rpx', 'color': '#2A2A2A'}"
			:confirm-style="{'font-size': '28rpx', 'color': '#2E73E5'}"
			:show-cancel-button="true"
			cancel-text="不同意"
			confirm-text="同意"
			@confirm="confirmAgreen"
			@cancel="cancelAgreen"
		>
			<view class="p-2">
				我们非常重视您的个人信息和隐私保护。依据最新法律要求，为向您提供更好的旅行服务，在使用我们的产品前，请您阅读完整版<text @click="toPage('/pages/user/about/privacyPolicy')" style="color: #2B85E4;">《隐私政策》</text>的所有条款。
			</view>
		</u-modal>
		<u-modal
			v-model="showTips"
			content="家多宝手持机账号不能在手机APP上登录!"
			title="提示"
			:title-style="{'font-size': '36rpx', 'color': '#2A2A2A'}"
			:content-style="{'font-size': '32rpx', 'color': '#4F5053'}"
			:confirm-style="{'font-size': '36rpx', 'color': '#2E73E5'}"
			confirm-text="确定"
			@confirm="showTips = false"
		></u-modal>
	</view>
</template>

<script>
	import faNavbar from '@/components/fa-navbar/fa-navbar.vue'
	export default {
		components: {
			faNavbar
		},
		data() {
			return {
				showAgreen: false,
				showTips: false,
				formData: {
					phone: '',
					password: ''
				},
				// formData: {
				// 	phone: 'user9527',
				// 	password: '123456'
				// },
			}
		},
		computed: {
			// 表单检验
			formValid() {
				// return true;
				return this.formData.phone.length == 11 && this.formData.password.length > 0
			}
		},
		onLoad() {
			if (!plus.runtime.isAgreePrivacy()) {
				this.showAgreen = true;
			}
		},
		methods: {
			login() {
				uni.showLoading({
				    title: '正在登录'
				});
				let params = {
					Username: this.formData.phone,
					Password: this.formData.password
				}
				this.$api.userLogin(params)
					.then(res => {
						if (res.code == 0) {
							uni.setStorageSync('TOKEN', res.data);
							uni.removeStorageSync('USER_ID');
							this.$api.getUserFuncLimit({})
								.then(res => {
									if (res.code == 0) {
										// 盘点是否有权限进入手机端页面
										for(let i = 0; i < res.data.length; i++) {
											if (res.data[i].indexOf("FASG_APP") > -1) {
												setTimeout(() => {
													uni.hideLoading();
													uni.switchTab({
														url: '/pages/index/index'
													})
												}, 500);
												return
											}
										}
										uni.hideLoading();
										uni.removeStorageSync('TOKEN');
										this.showTips = true;
									}else {
										uni.hideLoading();
									}
								})
								.catch(err => console.error(err));
						}else {
							uni.hideLoading();
						}
					})
					.catch(err => console.error(err));
			},
			
			toPage(url) {
				uni.navigateTo({
					url
				})
			},
			
			confirmAgreen() {
				// uni.setStorageSync('_USERAGREEN', '1'); // 同意隐私协议
				plus.runtime.agreePrivacy();
				this.showAgreen = false;
			},
			
			cancelAgreen() {
				if (plus.os.name.toLowerCase() === 'android') {
					plus.runtime.quit();
				}
				else{ 
					plus.ios.import('UIApplication').sharedApplication().performSelector('exit');
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	
</style>
